package tt.dz.order.util.thirdrecord;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import tt.dz.order.pojo.AliPayCallBackRecord;
import tt.dz.order.util.OrderConstant.OrderPayChannelCode;
import tt.dz.order.vo.ThdPayWayRecord;

/**
 *
 * 
 * @name ali第三方支付查询
 * @author Anson
 * @version 1.0
 * @summary 通过订单号查询第三方的支付记录
 * @desc
 *
 */
@Component
public class AliPayThirdRecordQuery implements ThirdRecordQuery {

	@Override
	public OrderPayChannelCode getThirdChannelCode() {
		return OrderPayChannelCode.alipay;
	}

	@Override
	public List<ThdPayWayRecord> query(List<String> orderNos) {
		List<AliPayCallBackRecord> originalRecords = this.queryAliPayRecord(orderNos);
		List<ThdPayWayRecord> records = new ArrayList<ThdPayWayRecord>(0);
		if (originalRecords == null || originalRecords.isEmpty()) {
			return records;
		}
		ThdPayWayRecord record = null;
		for (AliPayCallBackRecord r : originalRecords) {
			record = new ThdPayWayRecord();
			record.setOrderNo(r.getOrderNo());
			record.setTradeNo(r.getTradeNo());
			record.setTradeStatus(r.getTradeStatus());
			record.setPayAmount(new BigDecimal(r.getTotalFee()));
			record.setPayChannelCode(this.getThirdChannelCode().value());
			record.setQuantity(new BigDecimal(r.getQuantity()));
			record.setFinishTime(r.getGmtRefund());
			records.add(record);
		}

		return records;
	}

	/**
	 * 根据订单号查询支付宝回调信息
	 * 
	 * @param orderNos
	 * @return
	 */
	public List<AliPayCallBackRecord> queryAliPayRecord(List<String> orderNos) {
		return AliPayCallBackRecord.getByIds(orderNos);
	}

}
